#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from pymongo import MongoClient
import sys

default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)


# 连接MongoDB
def InitMongoDB():
    MONGODB_HOST = '10.188.188.100'
    MONGODB_PORT = 27017
    MONGODB_DB_NAME = 'node'
    MONGODB_COLLECTION_NAME = 'submitter'
    connection = MongoClient(MONGODB_HOST, MONGODB_PORT)
    db = connection[MONGODB_DB_NAME]
    collection = db[MONGODB_COLLECTION_NAME]
    return connection, collection


# 关闭MongoDB
def CloseMongoDB(MongoDB_Connection):
    MongoDB_Connection.close()

if __name__ == "__main__":
    MongoDBConnection, MongoDBCollection = InitMongoDB()
    ht={}
    for result in MongoDBCollection.find():
        firstname=result["first_name"]
        # middlename=result["middle_name"]
        lastname=result["last_name"]
        if firstname==None:
            firstname=''
        # if middlename==None:
        #     middlename=''
        if lastname==None:
            lastname=''
        combinename=firstname+"|"+lastname
        # orgname=result["org_name"]
        # deptname=""
        # if result.has_key("dept_name"):
        #     deptname=result["dept_name"]
        # piname=""
        # if result.has_key("piname"):
        #     piname=result["piname"]
        # if orgname==None:
        #     orgname=""
        # if deptname==None:
        #     deptname=""
        # if piname==None:
        #     deptname=""
        # combinename=orgname+"|"+deptname+"|"+piname
        if combinename=="":
            print "WAWAWAWA"+result["org_name"]
        if ht.has_key(combinename):
            ht[combinename]+=1
        else:
            ht[combinename]=1

    for key in ht.keys():
        print key+"\t"+str(ht[key])
    CloseMongoDB(MongoDBConnection)